import js from "@eslint/js";
import pluginVue from "eslint-plugin-vue";
import * as parserVue from "vue-eslint-parser";
import tseslint from "typescript-eslint";
import configPrettier from "eslint-config-prettier";
import pluginPrettier from "eslint-plugin-prettier";

export default [
    {
        ignores: [
            "**/.*",
            "dist/*",
            "*.d.ts",
            "public/*",
            "src/assets/**",
            "src/**/iconfont/**",
            "node_modules/**",
            "unpackage/**",
            "src/components/vk-uview-ui/**"
        ]
    },
    js.configs.recommended,
    {
        languageOptions: {
            globals: {
                // UniApp 全局 API
                uni: "readonly",
                wx: "readonly",
                plus: "readonly",
                getCurrentPages: "readonly",
                getApp: "readonly",
                UniApp: "readonly",
                UniNamespace: "readonly",
                Page: "readonly",
                App: "readonly",
                Component: "readonly",
                // 项目类型定义
                RefType: "readonly",
                EmitType: "readonly",
                TargetContext: "readonly",
                ComponentRef: "readonly",
                ElRef: "readonly",
                ForDataType: "readonly",
                AnyFunction: "readonly",
                PropType: "readonly",
                Writable: "readonly",
                Nullable: "readonly",
                NonNullable: "readonly",
                Recordable: "readonly",
                ReadonlyRecordable: "readonly",
                Indexable: "readonly",
                DeepPartial: "readonly",
                Without: "readonly",
                Exclusive: "readonly",
                TimeoutHandle: "readonly",
                IntervalHandle: "readonly",
                Effect: "readonly",
                ChangeEvent: "readonly",
                WheelEvent: "readonly",
                ImportMetaEnv: "readonly",
                Fn: "readonly",
                PromiseFn: "readonly",
                ComponentElRef: "readonly",
                parseInt: "readonly",
                parseFloat: "readonly"
            }
        },
        plugins: {
            prettier: pluginPrettier //将 Prettier 作为 ESLint 插件集成，让 ESLint 能够执行 Prettier 的代码格式化规则。
        },
        rules: {
            ...configPrettier.rules, // 关闭与 Prettier 冲突的 ESLint 规则
            ...pluginPrettier.configs.recommended.rules, // 启用 Prettier 推荐规则
            "no-debugger": "off",
            "no-unused-vars": [
                "error",
                {
                    argsIgnorePattern: "^_",
                    varsIgnorePattern: "^_"
                }
            ],
            "prettier/prettier": [
                // 配置 Prettier 格式化选项
                "error",
                {
                    endOfLine: "auto",
                    tabWidth: 4, //每个缩进的空格数
                    useTabs: false,
                    bracketSpacing: true,
                    singleQuote: false,
                    arrowParens: "avoid",
                    trailingComma: "none",
                    semi: true,
                    printWidth: 120,
                    htmlWhitespaceSensitivity: "ignore", //控制 HTML / Vue 模板中空格处理方式。
                    vueIndentScriptAndStyle: false, //控制 Vue 文件中 <script> 和 <style> 的缩进。
                    embeddedLanguageFormatting: "auto" //控制嵌入在模板中的代码是否格式化。
                }
            ]
        }
    },
    ...tseslint.config({
        extends: [...tseslint.configs.recommended],
        files: ["**/*.?([cm])ts", "**/*.?([cm])tsx"],
        rules: {
            "@typescript-eslint/no-redeclare": "error",
            "@typescript-eslint/ban-ts-comment": "off",
            "@typescript-eslint/no-explicit-any": "off",
            "@typescript-eslint/prefer-as-const": "warn",
            "@typescript-eslint/no-empty-function": "off",
            "@typescript-eslint/no-non-null-assertion": "off",
            "@typescript-eslint/no-unused-expressions": "off",
            "@typescript-eslint/no-unsafe-function-type": "off",
            "@typescript-eslint/no-import-type-side-effects": "error",
            "@typescript-eslint/explicit-module-boundary-types": "off",
            "@typescript-eslint/consistent-type-imports": [
                "error",
                {
                    disallowTypeAnnotations: false,
                    fixStyle: "inline-type-imports"
                }
            ],
            "@typescript-eslint/prefer-literal-enum-member": [
                "error",
                {
                    allowBitwiseExpressions: true
                }
            ],
            "@typescript-eslint/no-unused-vars": [
                "error",
                {
                    argsIgnorePattern: "^_",
                    varsIgnorePattern: "^_"
                }
            ]
        }
    }),
    {
        files: ["**/*.d.ts"],
        rules: {
            "eslint-comments/no-unlimited-disable": "off",
            "import/no-duplicates": "off",
            "no-restricted-syntax": "off",
            "unused-imports/no-unused-vars": "off"
        }
    },
    {
        files: ["**/*.?([cm])js"],
        rules: {
            "@typescript-eslint/no-require-imports": "off",
            "@typescript-eslint/no-var-requires": "off"
        }
    },
    ...pluginVue.configs["flat/recommended"],
    {
        files: ["**/*.vue"],
        languageOptions: {
            globals: {
                $: "readonly",
                $$: "readonly",
                $computed: "readonly",
                $customRef: "readonly",
                $ref: "readonly",
                $shallowRef: "readonly",
                $toRef: "readonly"
            },
            parser: parserVue,
            parserOptions: {
                ecmaFeatures: {
                    jsx: true
                },
                extraFileExtensions: [".vue"],
                parser: tseslint.parser,
                sourceType: "module"
            }
        },
        rules: {
            "no-undef": "off",
            "no-unused-vars": "off",
            "vue/no-v-html": "off",
            "vue/require-default-prop": "off",
            "vue/require-explicit-emits": "off",
            "vue/multi-word-component-names": "off",
            "vue/no-setup-props-reactivity-loss": "off",
            // ✅ 关闭 Vue 的模板换行缩进规则（交给 Prettier 控制）
            "vue/html-indent": "off",
            "vue/max-attributes-per-line": "off",
            "vue/first-attribute-linebreak": "off",
            "vue/singleline-html-element-content-newline": "off",
            "vue/multiline-html-element-content-newline": "off",
            "vue/html-closing-bracket-newline": "off",

            // ✅ 保留你已有的自闭合标签规则
            "vue/html-self-closing": [
                "error",
                {
                    html: { void: "always", normal: "never", component: "always" },
                    svg: "always",
                    math: "always"
                }
            ]
        }
    }
];
